package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.exception.ErrorCode;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.ProcResult;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.dto.PurchaseDTO;
import io.aircraft.modules.wms.service.PurchaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 入库单据
 *
 * @author cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-16
 */
@RestController
@RequestMapping("wms/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("page")
    @RequiresPermissions("wms:purchase:page")
    public Result<PageData<PurchaseDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<PurchaseDTO> page = purchaseService.page(params);

        return new Result<PageData<PurchaseDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:purchase:info")
    public Result<PurchaseDTO> get(@PathVariable("id") Long id) {
        PurchaseDTO data = purchaseService.get(id);

        return new Result<PurchaseDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:purchase:save")
    public Result save(@RequestBody PurchaseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        //1.校验同站台是否已有任务
        PurchaseDTO record = purchaseService.getListByStation(dto.getStationNo());
        if (record != null) {
            return new Result().error("目标站台已有入库计划!请优先处理");
        }
        purchaseService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:purchase:update")
    public Result update(@RequestBody PurchaseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        purchaseService.update(dto);

        return new Result();
    }

    @PostMapping("{id}")
    @RequiresPermissions("wms:purchase:done")
    public Result done(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);

        final ProcResult procResult = purchaseService.purchaseDone(id);
        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @DeleteMapping("{id}")
    @RequiresPermissions("wms:purchase:delete")
    public Result delete(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id);

        //调用单据取消逻辑
        ProcResult procResult = purchaseService.purchaseCancel(id);

        //处理结果
        if (!procResult.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, procResult.getMsg());
        } else
            return new Result().ok(procResult.getMsg());
    }

    @GetMapping("export")
    @LogOperation("导出")
    @RequiresPermissions("wms:purchase:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<PurchaseDTO> list = purchaseService.list(params);

//        ExcelUtils.exportExcelToTarget(response, null, list, PurchaseExcel.class);
    }

}